package com.br.jhone.accfc.util;

import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.br.jhone.accfc.R;

public class CursorItensAdapter extends CursorAdapter{

	private DBUtil mDbUtil;
	
	public CursorItensAdapter(Context context, Cursor c) {
		super(context, c);
		mDbUtil = new DBUtil(context);
	}


	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		LayoutInflater inflater = LayoutInflater.from(context);
        LinearLayout ll = (LinearLayout) inflater.inflate(R.layout.item_lista, parent, false);
        
        return ll;
	}

	@Override
	public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
	        String item = "";
	        if (constraint != null){
	            item = constraint.toString();       
	        }
	        return mDbUtil.listarItens(item);
	}

	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		LinearLayout ll = (LinearLayout) view;
		ImageView icone = (ImageView) ll.findViewById(R.id.imagemItem);
        Integer tipo = cursor.getInt(cursor.getColumnIndex(Cons.CAMPO_ID_TIPO));
        Boolean possui = cursor.getInt(cursor.getColumnIndex(Cons.CAMPO_POSSUI))>0;
        icone.setImageResource(UtilACCFC.recuperarDrawableRes(tipo, possui));
        
        TextView texto = (TextView) ll.findViewById(R.id.textoItem);
        texto.setText(cursor.getString(cursor.getColumnIndex(Cons.CAMPO_NOME)));
	}
}
